package com.nb.hdj.testCase.loginModule;

import com.nb.hdj.baseClass.SeleniumUtil;
import com.nb.hdj.moduleClass.loginModule.LoginAction;
import com.nb.hdj.page.LoginPage;
import com.nb.hdj.util.JsonDataSaveUtil;
import com.nb.hdj.util.YamlReadUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: TODO
 * @Author: hedongjia
 * @Date: 2021/11/5 上午11:20
 * @Version: v1.0
 */
public class CheckLogin {
    private static final Logger logger = LoggerFactory.getLogger(LoginAction.class);
    JsonDataSaveUtil jsonDataSaveUtil=new JsonDataSaveUtil();
    LoginPage loginPage=new LoginPage();
    SeleniumUtil seleniumUtil=new SeleniumUtil();
    /**
     * 打开aim-test2-service网址
     */
    public boolean openAimServiceUrl(WebDriver driver){
        try{
            String loginUrl =jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("loginUrl"));
            driver.get(loginUrl);
            logger.error("-----登录页正常----{}");
            return true;
        }catch (Exception e){
            e.fillInStackTrace();
            logger.error("-----登录页报错----{}",e.getMessage());
            return false;
        }
    }

    /**
     * 检查登录提示标题
     * @param driver
     * @return
     */
    public boolean LoginIframeCheck(WebDriver driver) {
        String  loginTitleElement= jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("loginTitleElement"));
        boolean check= seleniumUtil.elementTextIsEqual(driver, By.xpath(loginTitleElement), YamlReadUtil.getInstance().getValueByKey("Login", "title").toString());
        return check;
    }

    /**
     * 登录
     */
    public boolean InputNameAndPwdLogin(WebDriver driver) throws InterruptedException {
        String userName=jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("userName"));
        String passWord=jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("passWord"));
        String yzmButton=jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("yzmButton"));
        String yzm=jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("yzm"));
        String loginButton=jsonDataSaveUtil.ObjectToString(loginPage.getLoginPageElements().get("loginButton"));

        seleniumUtil.elementClick(driver,By.xpath(userName));
        seleniumUtil.elementClear(driver,By.xpath(userName));
        seleniumUtil.elementSendKeys(driver,By.xpath(userName),YamlReadUtil.getInstance().getValueByKey("Login","username").toString());


        seleniumUtil.elementClick(driver,By.xpath(passWord));
        seleniumUtil.elementClear(driver,By.xpath(passWord));
        seleniumUtil.elementSendKeys(driver,By.xpath(passWord),YamlReadUtil.getInstance().getValueByKey("Login","username").toString());
        return true;
    }

    public boolean loginCase(WebDriver driver){
        openAimServiceUrl(driver);
        LoginIframeCheck(driver);
        try {
            InputNameAndPwdLogin(driver);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }
}
